'''My server handler class.

It forbins user accessing directory directlly.

Note: This code only works in python3.
'''

# Athor : Hevake Lee 
# E-mail: hevake_lcj@126.com

# Logs
# --------
#  2015-02-22, Create MyHTTPRequestHandler.

import io
import sys
import platform

if platform.python_version().startswith('2'):
    raise Exception("http-server.py is not supported in python2.x")

import argparse
from http.server import CGIHTTPRequestHandler, test

class MyHTTPRequestHandler(CGIHTTPRequestHandler):
    '''MyHTTPRequestHandler is inherted from CGIHTTPServerHandler
    But, it avoids user from accessing directory.
    '''

    def list_directory(self, path):
        '''\
        Overwrite SimpleHTTPServer.list_directory() to avoid user from 
        accessing directory directly.'''

        self.send_error(404, "Directory is not accessable")
        return None

def run_server():
    parser = argparse.ArgumentParser()
    parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
                        help='Specify alternate bind address '
                             '[default: all interfaces]')
    parser.add_argument('port', action='store',
                        default=8000, type=int,
                        nargs='?',
                        help='Specify alternate port [default: 8000]')
    args = parser.parse_args()
    test(HandlerClass=MyHTTPRequestHandler, port=args.port, bind=args.bind)

if __name__ == '__main__':
    run_server()
